<template>
	<view class="scene-timing-wrap">
		<view class="container-wrap">
			<view class="form-wrap">
				<u--form :model="form" ref="form" :rules="rules" errorType="toast" labelPosition="left" labelWidth="80">
					<view class="form-item-wrap">
						<u-form-item prop="time" label="时间" @click="isTimer = true">
							<u--input v-model="form.time" border="none" inputAlign="right" placeholder="请选择时间"
								disabledColor="#fff" disabled :customStyle="{ marginRight: '20rpx' }" />
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</view>
					<view class="form-item-wrap">
						<u-form-item prop="repeat" label="重复" @click="isRepeat = true">
							<u--input v-model="form.repeat" border="none" inputAlign="right" placeholder="请选择星期"
								disabledColor="#fff" disabled :customStyle="{ marginRight: '20rpx' }" />
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</view>
					<view class="form-item-wrap">
						<u-form-item label="CRON">
							<template slot="right">
								<u-radio-group v-model="form.isAdvance">
									<u-radio :customStyle="{marginRight: '30rpx'}" label="默认" :name="0"></u-radio>
									<u-radio :customStyle="{marginRight: '10rpx'}" label="自定义" :name="1"
										disabled></u-radio>
								</u-radio-group>
							</template>
						</u-form-item>
					</view>
					<view class="form-item-wrap">
						<u-form-item>
							<!-- #ifndef APP-NVUE -->
							<u-input v-model="form.cronExpression" placeholder="请输入CRON" disabled>
							<!-- #endif -->
								<!-- #ifdef APP-NVUE -->
								<u--input v-model="form.cronExpression" placeholder="请输入CRON" disabled>
								<!-- #endif -->
									<template slot="suffix">
										<u-button text="生成表达式" icon="calendar" type="primary" size="mini"
											disabled></u-button>
									</template>
							<!-- #ifndef APP-NVUE -->
							</u-input>
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							</u--input>
							<!-- #endif -->
						</u-form-item>
					</view>
				</u--form>
			</view>
			<view class="btn-wrap">
				<u-button :customStyle="{ height: '96rpx' }" size="normal" :plain="true" text="保存"
					@click="handleSave"></u-button>
			</view>
		</view>
		<view class="other">
			<!-- 选择时间 -->
			<u-datetime-picker :show="isTimer" v-model="form.time" mode="time" @cancel="isTimer=false"
				@close="isTimer=false" @confirm="handleDatetimeConfirm"></u-datetime-picker>

			<!-- 星期 -->
			<u-popup :show="isRepeat" :round="5" mode="bottom" bgColor="#eef3f7" :closeOnClickOverlay="true"
				@close="isRepeat = false">
				<view class="repeat-popup-wrap">
					<view class="nav">
						<text @click="isRepeat = false">取消</text>
						<text @click="handleRepeatConfirm">确认</text>
					</view>
					<view class="radio-group-wrap">
						<u-checkbox-group v-model="repeatValue" :borderBottom="false" placement="column"
							iconPlacement="right">
							<view class="radio-wrap">
								<u-checkbox label="周一" :name="1" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周二" :name="2" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周三" :name="3" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周四" :name="4" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周五" :name="5" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周六" :name="6" iconSize="16"></u-checkbox>
							</view>
							<view class="radio-wrap">
								<u-checkbox label="周日" :name="7" iconSize="16"></u-checkbox>
							</view>
						</u-checkbox-group>
					</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import { navigateBackTo } from '@/utils/common.js';

	export default {
		data () {
			return {
				type: 'trigger',
				editIndex: null, // null 代表新增
				isTimer: false, // 时间
				isRepeat: false, // 星期
				form: {
					time: uni.$u.timeFormat(new Date(), 'hh:MM'),
					repeat: '每天',
					isAdvance: 0,
					cronExpression: `0 ${uni.$u.timeFormat(new Date(),'MM')} ${uni.$u.timeFormat(new Date(),'hh')} ? * 1,2,3,4,5,6,7`,
				},
				rules: {
					time: {
						type: 'string',
						required: true,
						message: '请选择时间',
						trigger: ['blur', 'change']
					},
					repeat: {
						type: 'string',
						required: true,
						message: '请选择星期',
						trigger: ['blur', 'change']
					},
				},
				repeatValue: [1, 2, 3, 4, 5, 6, 7],
			};
		},
		onLoad (option) {
			const { type, editIndex } = option;
			this.type = type;
			this.editIndex = Number(editIndex);
			if (!Number.isNaN(this.editIndex) && this.editIndex !== null) {
				const { cronExpression } = uni.getStorageSync('trigger');
				const time = cronExpression.substring(5, 7) + ':' + cronExpression.substring(2, 4);
				const repeatValue = cronExpression.substring(12).split(',').map(Number);
				const repeat = this.getRepeatName(repeatValue)
				this.form = { ...this.form, time, repeat, cronExpression };
				this.repeatValue = repeatValue;
			}
		},
		methods: {
			// 时间选择
			handleDatetimeConfirm () {
				this.$nextTick(() => {
					this.gentCronExpression();
				})
				this.isTimer = false;
			},
			// 星期选择
			handleRepeatConfirm () {
				const repeat = this.getRepeatName(this.repeatValue);
				this.form.repeat = repeat;
				this.gentCronExpression();
				this.isRepeat = false;
			},
			// 获取repeat名称
			getRepeatName (repeatValue) {
				const list = repeatValue.map(item => {
					if (item === 1) {
						return '周一'
					} else if (item === 2) {
						return '周二'
					} else if (item === 3) {
						return '周三'
					} else if (item === 4) {
						return '周四'
					} else if (item === 5) {
						return '周五'
					} else if (item === 6) {
						return '周六'
					} else if (item === 7) {
						return '周日'
					}
				})
				if (list.length === 7) {
					return '每天';
				} else {
					return list.join(',');
				}
			},
			// 生成cron表达式
			gentCronExpression () {
				let hour = '00';
				let minute = '00';
				if (this.form.time) {
					hour = this.form.time.substring(0, 2);
					minute = this.form.time.substring(3);
				}
				let week = '*';
				if (this.repeatValue.length > 0) {
					week = this.repeatValue.sort();
				}
				this.form.cronExpression = '0 ' + minute + ' ' + hour + ' ? * ' + week;
			},
			// 保存
			handleSave () {
				this.$refs.form.validate().then(respon => {
					let action = uni.getStorageSync(this.type);
					const { cronExpression } = this.form;
					action = { ...action, cronExpression };
					uni.setStorageSync(this.type, action);
					// 更新或者插入新的触发或者执行
					let { triggers, actions, ...res } = uni.getStorageSync('sceneData');
					if (Number.isNaN(this.editIndex) || this.editIndex === null) {
						if (this.type === 'trigger') {
							triggers.push(action);
						} else {
							actions.push(action);
						}
						uni.setStorageSync('sceneData', { triggers, actions, ...res });
					} else {
						if (this.type === 'trigger') {
							let list = triggers.map((item, i) => {
								if (i == this.editIndex) {
									return action
								} else {
									return item
								}
							});
							uni.setStorageSync('sceneData', { triggers: [...list], actions, ...res });
						} else {
							let list = actions.map((item, i) => {
								if (i == this.editIndex) {
									return action
								} else {
									return item
								}
							});
							uni.setStorageSync('sceneData', { triggers, actions: [...list], ...res });
						}
					}
					uni.setStorageSync('callback', true);
					navigateBackTo('/pagesA/scene/detail');
				})
			}
		}
	};
</script>

<style>
	page {
		height: 100%;
		background: #eef3f7;
	}
</style>
<style lang="scss" scoped>
	.scene-timing-wrap {

		.container-wrap {
			.form-wrap {
				background: #fff;
				margin: 30rpx;

				.form-item-wrap {
					padding: 4rpx 20rpx;
				}
			}

			.btn-wrap {
				margin: 30rpx 26rpx;
			}
		}

		.other {
			.repeat-popup-wrap {
				padding: 30rpx;

				.nav {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					font-size: 32rpx;
					margin-bottom: 34rpx;
				}

				.radio-group-wrap {
					background-color: #fff;
					border-radius: 10rpx;

					.radio-wrap {
						padding: 40rpx;

						&:not(:last-child) {
							border-bottom: 1rpx solid #F1F2F5;
						}
					}
				}
			}
		}
	}
</style>